package com.axs.camera;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.axs.camera.adapter.ResultAdapter;
import com.axs.camera.bean.PhotoResultBean;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jeremyliao.liveeventbus.LiveEventBus;

import java.time.Instant;
import java.util.ArrayList;
import java.util.List;

public class ResultActivity extends AppCompatActivity {
    private RecyclerView rvList;
    private Toolbar toolbar;

    private List<PhotoResultBean.ResultDTO> mList = new ArrayList<>();
    private ResultAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_result);


        toolbar = findViewById(R.id.toolbar);
        toolbar.setNavigationOnClickListener(v -> finish());

        rvList = findViewById(R.id.rv_list);
        adapter = new ResultAdapter(mList, this);
        rvList.setAdapter(adapter);


        LiveEventBus.get("result", String.class)
                .observeSticky(this, s -> {
                    List<PhotoResultBean.ResultDTO> list = new Gson().fromJson(s, new TypeToken<List<PhotoResultBean.ResultDTO>>() {
                    }.getType());

                    mList.clear();
                    mList.addAll(list);
                    adapter.notifyDataSetChanged();
                });

        adapter.setOnItemClickListener(bean -> {
            //拿到item的bean
            Intent instant = new Intent(ResultActivity.this,WebViewActivity.class);
            instant.putExtra("beanBaidu",bean.getBaikeInfo().getBaikeUrl());
            startActivity(instant);
        });

    }
}